package com.lenovo.lcr.sil.wangdwa.learning;

import java.util.ArrayList;
import java.util.HashMap;
import android.app.ExpandableListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.SimpleExpandableListAdapter;
import android.widget.Toast;

public class SimpleELVActivity extends ExpandableListActivity {
	
	private ArrayList<HashMap<String, String>> mGroups = 
		new ArrayList<HashMap<String, String>>();
	
	private ArrayList<ArrayList<HashMap<String, String>>> mPeople = 
		new ArrayList<ArrayList<HashMap<String, String>>>();
	
	
	private String[] mDetailKeys = {
			People.KEY1,
			People.KEY2
	};
	
	
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        initChildren();
        
        ExpandableListAdapter adapter = new SimpleExpandableListAdapter(
        		this, 
        		mGroups, 
        		android.R.layout.simple_expandable_list_item_1,
        		new String[] {Group.KEY}, 
        		new int[] {android.R.id.text1},
        		mPeople, 
        		android.R.layout.simple_expandable_list_item_2, 
        		mDetailKeys, 
        		new int[]{android.R.id.text1, android.R.id.text2}
        		);
        
        this.setListAdapter(adapter);
        
    }



	@Override
	public boolean onChildClick(ExpandableListView parent, View v,
			int groupPosition, int childPosition, long id) {
		// TODO Auto-generated method stub
		People p = new People(mPeople.get(groupPosition).get(childPosition));
		Toast.makeText(this, p.toString(), Toast.LENGTH_LONG).show();
		return super.onChildClick(parent, v, groupPosition, childPosition, id);
	}



	private void initChildren() {
		// TODO Auto-generated method stub
		
		Group group1 = new Group("Classmates");
		mGroups.add(group1.getData());		
		PeopleGroup pg1 = new PeopleGroup();		
		People p1 = new People("Zhangsan", "139-1234-5678");
		pg1.addPeople(p1);
		People p2 = new People("Lisi", "138-7896-1000");
		pg1.addPeople(p2);
		mPeople.add(pg1.getData());
		
		Group group2 = new Group("Friends");
		mGroups.add(group2.getData());
		PeopleGroup pg2 = new PeopleGroup();		
		People p3 = new People("Wangwu", "136-5678-0988");
		pg2.addPeople(p3);
		People p4 = new People("Zhaoliu", "134-4567-1234");
		pg2.addPeople(p4);
		mPeople.add(pg2.getData());
		
		 
	}
}